package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementStationAlarmBo;
import com.ruoyi.zhgd.domain.vo.NCementStationAlarmVo;
import com.ruoyi.zhgd.domain.NCementStationAlarm;
import com.ruoyi.zhgd.mapper.NCementStationAlarmMapper;
import com.ruoyi.zhgd.service.INCementStationAlarmService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 水稳拌合站报警数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementStationAlarmServiceImpl implements INCementStationAlarmService {

    private final NCementStationAlarmMapper baseMapper;

    /**
     * 查询水稳拌合站报警数据
     */
    @Override
    public NCementStationAlarmVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站报警数据列表
     */
    @Override
    public TableDataInfo<NCementStationAlarmVo> queryPageList(NCementStationAlarmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementStationAlarm> lqw = buildQueryWrapper(bo);
        Page<NCementStationAlarmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站报警数据列表
     */
    @Override
    public List<NCementStationAlarmVo> queryList(NCementStationAlarmBo bo) {
        LambdaQueryWrapper<NCementStationAlarm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementStationAlarm> buildQueryWrapper(NCementStationAlarmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementStationAlarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NCementStationAlarm::getStationId, bo.getStationId());
        lqw.eq(StringUtils.isNotBlank(bo.getGravel1Alarm()), NCementStationAlarm::getGravel1Alarm, bo.getGravel1Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getGravel2Alarm()), NCementStationAlarm::getGravel2Alarm, bo.getGravel2Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getGravel3Alarm()), NCementStationAlarm::getGravel3Alarm, bo.getGravel3Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getGravel4Alarm()), NCementStationAlarm::getGravel4Alarm, bo.getGravel4Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getSand1Alarm()), NCementStationAlarm::getSand1Alarm, bo.getSand1Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getSand2Alarm()), NCementStationAlarm::getSand2Alarm, bo.getSand2Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getCement1Alarm()), NCementStationAlarm::getCement1Alarm, bo.getCement1Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getCement2Alarm()), NCementStationAlarm::getCement2Alarm, bo.getCement2Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterial9Alarm()), NCementStationAlarm::getMaterial9Alarm, bo.getMaterial9Alarm());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarm()), NCementStationAlarm::getAlarm, bo.getAlarm());
        return lqw;
    }

    /**
     * 新增水稳拌合站报警数据
     */
    @Override
    public Boolean insertByBo(NCementStationAlarmBo bo) {
        NCementStationAlarm add = BeanUtil.toBean(bo, NCementStationAlarm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站报警数据
     */
    @Override
    public Boolean updateByBo(NCementStationAlarmBo bo) {
        NCementStationAlarm update = BeanUtil.toBean(bo, NCementStationAlarm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementStationAlarm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站报警数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
